<template>
  <Modal
    title="编辑"
    :value="value"
    :mask-closable="true"
    :footer-hide="true"
    @input="$emit('input', $event)"
  >
    <Form
      ref="formLkyAssessment"
      :model="formLkyAssessment"
      :rules="ruleValidate"
      label-position="right"
      :label-width="80"
    >
      <FormItem label="实训项目" prop="training_project">
        <Input type="text" v-model="formLkyAssessment.training_project" placeholder="实训项目"/>
      </FormItem>
      <FormItem label="开始时间" prop="start_time">
        <Input type="text" v-model="formLkyAssessment.start_time" placeholder="开始时间"/>
      </FormItem>
      <FormItem label="结束时间" prop="end_time">
        <Input type="text" v-model="formLkyAssessment.end_time" placeholder="结束时间"/>
      </FormItem>
      <FormItem label="实训地点" prop="training_site">
        <Input type="text" v-model="formLkyAssessment.training_site" placeholder="实训地点"/>
      </FormItem>
	  <FormItem label="指导老师" prop="guidance_teacher">
	    <Input type="text" v-model="formLkyAssessment.guidance_teacher" placeholder="指导老师"/>
	  </FormItem>
	  <FormItem label="分值" prop="score">
	    <Input type="text" v-model="formLkyAssessment.score" placeholder="分值"/>
	  </FormItem>
      <FormItem>
        <Button type="primary" @click="handleSubmit('formLkyAssessment')">提交</Button>
        <Button @click="handleReset('formLkyAssessment')" style="margin-left: 8px">重置</Button>
      </FormItem>

    </Form>
  </Modal>
</template>
<script>
import { update } from '@/api/lky/assessment'
import { createModelObj, coverProps, coverFormProps } from '@/libs/util'
// 学院查询、专业查询
import {queryAll} from '@/api/lky/lkyCollege'
import {queryAllM} from '@/api/lky/lkyMajor'

export default {
  name: 'Edit',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    lkyAssessment: {
      type: Object,
      default: null
    },
    lkyEvaluation: {
	  type: Object,
	  default: null
    }
  },
  data () {
    return {
      collegeList: [],
      coid: '',
      // 这个是修改的
      mid: '',
      majorList: [],

      formLkyAssessment: {
        id: '',
        training_project: '',
        start_time: '',
        end_time: '',
        training_site: '',
        guidance_teacher: '',
        score: ''
      },
      ruleValidate: {

        training_project: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 20, message: '长度最长为20', trigger: 'blur' }
        ],
        start_time: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 20, message: '长度最长为20', trigger: 'blur' }
        ],
        end_time: [
          { required: false, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 32, message: '长度最长为32', trigger: 'blur' }
        ],
        training_site: [
		  { required: false, message: '该值不能为空', trigger: 'blur' },
		  { type: 'string', max: 32, message: '长度最长为32', trigger: 'blur' }
        ],
        guidance_teacher: [
		  { required: false, message: '该值不能为空', trigger: 'blur' },
		  { type: 'string', max: 32, message: '长度最长为32', trigger: 'blur' }
        ],
        score: [
          { required: false, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 32, message: '长度最长为32', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onOpenChangeCollege () {
      queryAll().then(res => {
        // console.log(res)
        this.collegeList = res.data
        // this.formLkyAssessment.mid = ''
      })
    },
    onOpenChangeMajor () {
      queryAllM({'coid': this.coid}).then(res => {
        this.majorList = res.data
      })
    },
    handleSubmit (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          update(
            Object.assign({}, createModelObj(this.formLkyAssessment, 'lkyEvaluation'))
          ).then(res => {
            if (res) {
              this.$emit('updateRow', coverProps(this.lkyEvaluation, this.formLkyAssessment))
              this.$emit('input', false)
              this.handleReset(name)
            }
          })
        } else {
          this.$Message.error('验证失败!')
        }
      })
    },
    handleReset (name) {
      this.$refs[name].resetFields()
    }
  },
  watch: {
    value: function (val, oldVal) {
      this.$refs.formLkyAssessment.resetFields()
      coverFormProps(this.formLkyAssessment, this.lkyAssessment)
    }
  },
  // 这里
  mounted () {
    this.onOpenChangeCollege()
  }
}
</script>
